<?php
namespace Orx\Mailer;
/**
 * E-Mail Box Manager
 * 
 * This class should be used to read using POP3 an email box
 * and display contained messages.
 * 
 * 
 */
class MailBoxManager {
	
	/**
	 * Parameter to pass in the Service host.
	 *
	 * @var string
	 */
	const PARAM_HOST = "host";
	
	/**
	 * Parameter to pass in a username.
	 *
	 * @var string
	 */
	const PARAM_USER = "user";

	/**
	 * Parameter to pass in a username.
	 *
	 * @var string
	 */
	const PARAM_PASSWORD = "password";
	
	/**
	 * Parameter to pass in the Service port.
	 *
	 * @var string
	 */
	const PARAM_PORT = "port";
	
	/**
	 * Parameter to pass in the SSL/TLS connection
	 *
	 * @var string
	 */
	const PARAM_SSL = "ssl";
	
	/**
	 * Storage Class 
	 * @var string
	 */
	const PARAM_STORAGE_CLASS = 'storage_class';
	
		
	/**
	 * The mail box lise
	 *
	 * @var array id => object
	 */
	protected $mailboxes = array();

	/**
	 * Singleton instance
	 *
	 * @var \Orx\MailBoxManager
	 */
	private static $_instance;
	
	/**
	 * Retrieve singleton instance
	 *
	 * @return MailBoxManager
	 */
	public static function instance()
	{
		if (null === self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	/**
	 * Register a Mail Box
	 * @param array
	 */
	public function register($data = null, $_ = null) {
		foreach (func_get_args() as $data)
		{
			foreach($data as $name => $params) {
				if(isset($this->mailboxes[$name])) {
					// Already Register
				} else {
					if(isset($params[self::PARAM_SSL])) {
						$ssl = $params[self::PARAM_SSL];
					} else {
						$ssl = false;
					}
					$classname = $params[self::PARAM_STORAGE_CLASS];
					$this->mailboxes[$name] = new $classname($params[self::PARAM_HOST],
														$params[self::PARAM_PORT],
														$params[self::PARAM_USER],
														$params[self::PARAM_PASSWORD],
														$ssl);
				}
			}
		}
		return $this;
	}
	
	/**
	 * Find and return selected mail box
	 * @return MailBox
	 */
	public function box($name) {
		return $this->mailboxes[$name];
	}
	
	/**
	 * Check if the mail box is already registered
	 * @param $name Mail Box Unique Id
	 * @return bool
	 */
	public function isRegistered($name) {
		if(is_array($this->mailboxes) && isset($this->mailboxes[$name])) {
			return true;
		} else {
			return false;
		}
	}
}